clear
clear matrix
clear mata
set more off, perm
set matsize 11000
set maxvar 30000
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/

// open visit dataset
use clean/radiology_visit_3.dta, clear

// Make treatment indicator
gen post = (ym>=ym(2007,3))
gen web_post = (visit_on_web==1 & post==1)
label var web_post "$\text{OnWeb}\textsubscript{m}\times\text{Post}\textsubscript{t}$"


// Set X controls 
global ctr_vars1 = "has_copay has_coin##deduct_type age male charlindex zip_mean_income zip_pct_ba i.rural_class i.ym i.std_product_type i.payer_id"
global ctr_vars2 = "age male charlindex zip_mean_income zip_pct_ba i.rural_class i.ym i.std_product_type i.payer_id"


// Make aggregate dataset
preserve
gen n=1
xi i.std_product_type i.payer_id i.rural_class i.charlindex
fcollapse (sum) n (mean) ln* amt_allowed amt_member_oop amt_paid ///
	has_copay deduct_type age male zip_mean_income zip_pct_ba _I* ///
	, by(web_post proc_id_radio ym year) fast
save clean/radiology_visit_3_aggregated.dta, replace
restore

////////////////////
// PANEL 1 (Total Amount)
////////////////////


// No Controls
areg ln_amt_allowed web_post i.ym, absorb(proc_id_radio) vce(cl ym) 
estimates esample:  ln_amt_member_oop web_post ym proc_id_radio, replace
summ amt_allowed if e(sample)
estadd scalar ymeanexp = r(mean), replace
eststo a1


// No Insurer controls
areg ln_amt_allowed web_post has_copay has_coin##deduct_type age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.ym , absorb(proc_id_radio) vce(cl ym) 
summ amt_allowed if e(sample)
estadd scalar ymeanexp = r(mean), replace
eststo a2

// Interact Insurer FE with Post
gen payer_id_post = payer_id
replace payer_id_post = 0 if post==0
areg ln_amt_allowed web_post has_copay has_coin##deduct_type age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.ym i.std_product_type i.payer_id i.payer_id_post, absorb(proc_id_radio) vce(cl ym) 
summ amt_allowed if e(sample)
estadd scalar ymeanexp = r(mean), replace
eststo a3


// Member FE
preserve
keep if mm_start_yearmo<200603 & mm_end_yearmo>200803
count
areg ln_amt_member_oop web_post age i.ym i.std_product_type i.payer_id i.proc_id_radio, absorb(member_key) vce(cl ym) 
summ amt_allowed if ~missing(web_post,member_key,ym,proc_id_radio,has_copay,has_coin,deduct_type,age,male,charlindex,zip_mean_income,zip_pct_ba,rural_class,std_product_type,payernm_short,payer_id), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a5
restore

// Weighted OLS
preserve
use clean/radiology_visit_3_aggregated.dta, clear
areg ln_amt_allowed web_post i.proc_id_radio has_copay age male zip_mean_income zip_pct_ba _I* [fw=n], absorb(ym) vce(cl ym)
summ amt_allowed if e(sample) [fw=n]
estadd scalar ymeanexp = r(mean), replace
count if e(sample)
estadd scalar N = r(N), replace
eststo a6
restore

// Weighted GLS
preserve
use clean/radiology_visit_3_aggregated.dta, clear
glm amt_allowed web_post i.proc_id_radio i.ym has_copay age male zip_mean_income zip_pct_ba _I* [fw=n], family(poisson) link(log) search vce(cl ym)
summ amt_allowed if e(sample) [fw=n]
estadd scalar ymeanexp = r(mean), replace
count if e(sample)
estadd scalar N = r(N), replace
eststo a7
restore




esttab a1 a2 a3 a5 a6 a7 using $output/robustness_regs_a.tex, frag cells(b(fmt(3) star) se(fmt(3) par)) ///
	keep(web_post)  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
esttab a1 a2 a3 a5 a6 a7 using $output/robustness_regs_b.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(ymeanexp ar2 N, fmt(2 3 %12.0fc) labels("Mean level" "Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 



////////////////////
// PANEL 2 (OOP)
////////////////////


// No Controls
areg ln_amt_member_oop web_post i.ym, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample)
estadd scalar ymeanexp = r(mean), replace
eststo b1


// No Insurer controls
areg ln_amt_member_oop web_post has_copay has_coin##deduct_type age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.ym , absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample)
estadd scalar ymeanexp = r(mean), replace
eststo b2

// Interact Insurer FE with Post
areg ln_amt_member_oop web_post has_copay has_coin##deduct_type age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.ym i.std_product_type i.payer_id i.payer_id_post, absorb(proc_id_radio) vce(cl ym) 
summ amt_member_oop if e(sample)
estadd scalar ymeanexp = r(mean), replace
eststo b3


// Member FE
preserve
keep if mm_start_yearmo<200603 & mm_end_yearmo>200803
count
areg ln_amt_member_oop web_post age i.ym i.std_product_type i.payer_id i.proc_id_radio, absorb(member_key) vce(cl ym) 
summ amt_member_oop if e(sample)
estadd scalar ymeanexp = r(mean), replace
eststo b5
restore

// Weighted OLS
preserve
use clean/radiology_visit_3_aggregated.dta, clear
areg ln_amt_member_oop web_post i.proc_id_radio has_copay age male zip_mean_income zip_pct_ba _I* [fw=n], absorb(ym) vce(cl ym)
summ amt_member_oop if e(sample) [fw=n]
estadd scalar ymeanexp = r(mean), replace
count if e(sample)
estadd scalar N = r(N), replace
eststo b6
restore

// Weighted GLS
preserve
use clean/radiology_visit_3_aggregated.dta, clear
glm amt_member_oop web_post i.proc_id_radio i.ym has_copay age male zip_mean_income zip_pct_ba _I* [fw=n], family(poisson) link(log) search vce(cl ym)
summ amt_member_oop if e(sample) [fw=n]
estadd scalar ymeanexp = r(mean), replace
count if e(sample)
estadd scalar N = r(N), replace
eststo b7
restore

esttab b1 b2 b3 b5 b6 b7 using $output/robustness_regs_c.tex, frag cells(b(fmt(3) star) se(fmt(3) par)) ///
	keep(web_post)  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
esttab b1 b2 b3 b5 b6 b7 using $output/robustness_regs_d.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(ymeanexp ar2 N, fmt(2 3 %12.0fc) labels("Mean level" "Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 


////////////////////
// PANEL 3 (PAID)
////////////////////

// No Controls

areg ln_amt_paid web_post i.ym, absorb(proc_id_radio) vce(cl ym) 
summ amt_paid if ~missing(web_post,ym,proc_id_radio), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo c1

// No Insurer controls
areg ln_amt_paid web_post has_copay has_coin##deduct_type age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.ym , absorb(proc_id_radio) vce(cl ym) 
summ amt_paid if ~missing(web_post,ym,proc_id_radio,has_copay,has_coin,deduct_type,age,male,charlindex,zip_mean_income,zip_pct_ba,rural_class), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo c2

// Interact Insurer FE with Post
areg ln_amt_paid web_post has_copay has_coin##deduct_type age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.ym i.std_product_type i.payer_id i.payer_id_post, absorb(proc_id_radio) vce(cl ym) 
estimates esample:  ln_amt_member_oop web_post has_copay has_coin deduct_type age male charlindex zip_mean_income zip_pct_ba rural_class ym std_product_type payer_id payer_id_post proc_id_radio, replace
summ amt_paid if e(sample)
estadd scalar ymeanexp = r(mean), replace
eststo c3

// Member FE
preserve
keep if mm_start_yearmo<200603 & mm_end_yearmo>200803
count
areg ln_amt_paid web_post age i.ym i.std_product_type i.payer_id i.proc_id_radio, absorb(member_key) vce(cl ym) 
summ amt_paid if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo c5
restore


// Weighted OLS
preserve
use clean/radiology_visit_3_aggregated.dta, clear
areg ln_amt_paid web_post i.proc_id_radio has_copay age male zip_mean_income zip_pct_ba _I* [fw=n], absorb(ym) vce(cl ym)
summ amt_paid if e(sample) [fw=n]
estadd scalar ymeanexp = r(mean), replace
count if e(sample)
estadd scalar N = r(N), replace
eststo c6
restore

// Weighted GLS
preserve
use clean/radiology_visit_3_aggregated.dta, clear
glm amt_paid web_post i.proc_id_radio i.ym has_copay age male zip_mean_income zip_pct_ba _I* [fw=n], family(poisson) link(log) search vce(cl ym)
summ amt_paid if e(sample) [fw=n]
estadd scalar ymeanexp = r(mean), replace
count if e(sample)
estadd scalar N = r(N), replace
eststo c7
restore

esttab c1 c2 c3 c5 c6 c7 using $output/robustness_regs_e.tex, frag cells(b(fmt(3) star) se(fmt(3) par)) ///
	keep(web_post)  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
esttab c1 c2 c3 c5 c6 c7 using $output/robustness_regs_f.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(ymeanexp ar2 N, fmt(2 3 %12.0fc) labels("Mean level" "Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 
	



